package de.koller.aframe.gl;

import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.gl.util.IOceanShaderProgram;
import de.koller.oceanFramework.gl.util.TextureUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class FileShaderProgram implements IOceanShaderProgram {

	private final String vertexShader;
	private final String fragmentShader;
	
	public FileShaderProgram( String vertexShaderFile, String fragmentShaderFile ) {
		try {
			this.vertexShader = readAll( vertexShaderFile );
			this.fragmentShader = readAll( fragmentShaderFile );
		} catch( IOException ex ) {
			throw new OceanException( ex );
		}
	}
	
	private String readAll( String file ) throws IOException {
		ClassLoader cl = TextureUtil.class.getClassLoader();
		InputStream in = cl.getResourceAsStream( file );
		BufferedReader r = new BufferedReader( new InputStreamReader( in ) );
		
		String text = "";
		String s = r.readLine();
		while( s != null ) {
			text += "\n" + s;
			s = r.readLine();
		}
		
		return text;
	}
	
	@Override
	public String getVertexShader() {
		return vertexShader;
	}

	@Override
	public String getFragmentShader() {
		return fragmentShader;
	}
	
}
